﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.crm
{
    public partial class qunfa_history : Web.UI.ManagePage
    {
         

        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
           
            this.keywords = MXRequest.GetQueryString("keywords");
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("qunfa_history", MXEnums.ActionEnum.View.ToString()); //检查权限
                RptBind(CombSqlTxt(this.keywords), "id desc");
            }
        }

        //数据绑定
        private void RptBind(string _strWhere, string _orderby)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

            this.page = MXRequest.GetQueryInt("page", 1);

            this.txtKeywords.Text = this.keywords;

            BLL.wx_qunfa_history bll = new BLL.wx_qunfa_history();
            DataSet ds = bll.GetQunfaHistoryList(weixin.id, this.pageSize, this.page, _strWhere, _orderby, out totalCount);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dr = ds.Tables[0].Rows[i];
                    if (dr["sendStyle"].ToString() == "1")
                    {
                        dr["sendStylestr"] = "按组发送";
                    }
                    else if (dr["sendStyle"].ToString() == "2")
                    {
                        dr["sendStylestr"] = "根据openid发送";
                    }
                    else if (dr["sendStyle"].ToString() == "3")
                    {
                        dr["sendStylestr"] = "全部用户";
                    }
                    
                }
            }
            this.rptList.DataSource = ds;
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("qunfa_history.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);

        }

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("   title like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion


        #region 返回图文分类每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("qunfa_history_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion


        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("qunfa_history.aspx", "keywords={0}", txtKeywords.Text));
        }


        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("qunfa_history_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("qunfa_history.aspx", "keywords={0}", this.keywords));
        }



      

        
    }
}